require 'test/unit'
require_relative 'sample_tlv'
require 'lib/smppclient/util/byte_buffer'

class TestTlv < MiniTest::Unit::TestCase

  include SmppClient::Pdu::Tlv
  include SmppClient::Util

  def test_bytes_to_object
    bb = ByteBuffer.new
    bb.append_short(0x5555)
    bb.append_short(0x0001)
    bb.append_byte(10)

    st = SampleTlv.new(bb)
    assert_equal(SampleTlv::TAG, st.tag)
    assert_equal(1, st.length)
    assert_equal(10, st.value)
    assert_equal('555500010a', st.buffer.to_s)
  end

  def test_bytes_to_object_wrong_length
    bb = ByteBuffer.new
    bb.append_short(0x5555)
    bb.append_short(0x0002)
    bb.append_byte(10)

    assert_raises(TlvError) { SampleTlv.new(bb) }
  end

  def test_object_to_bytes
    st = SampleTlv.new(2)
    assert_equal('5555000102', st.buffer.to_s)
    assert_equal(SampleTlv::TAG, st.tag)
    assert_equal(1, st.length)
    assert_equal(2, st.value)
  end

end